package com.code.wflow.service.impl;

import com.code.wflow.handler.InstanceActuator;
import com.code.wflow.mapper.InstanceMapper;
import com.code.wflow.mapper.InstanceNodeDetailMapper;
import com.code.wflow.mapper.InstanceNodeMapper;
import com.code.wflow.mapper.WorkflowTaskMapper;
import com.code.wflow.model.Instance;
import com.code.wflow.model.InstanceNode;
import com.code.wflow.model.InstanceNodeDetail;
import com.code.wflow.model.WorkflowTask;
import com.code.wflow.service.WorkflowTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 流程任务表(WorkflowTask)表服务层
 *
 * @author ggh
 * @date 2022/11/10
 */
@Service
public class WorkflowTaskServiceImpl implements WorkflowTaskService {
    @Autowired
    private WorkflowTaskMapper workflowTaskMapper;
    @Autowired
    private InstanceNodeDetailMapper instanceNodeDetailMapper;
    @Autowired
    private InstanceNodeMapper instanceNodeMapper;
    @Autowired
    private InstanceMapper instanceMapper;
    @Autowired
    private InstanceActuator instanceActuator;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public WorkflowTask queryById(Long id) {
        return this.workflowTaskMapper.queryById(id);
    }

    /**
     * 查询指定行数据
     *
     * @param userId
     * @return
     */
    @Override
    public List<WorkflowTask> queryByUserId(Long userId, Byte type, Boolean finish) {
        return this.workflowTaskMapper.queryByUserId(userId, type, finish);
    }

    /**
     * 根据实例id查询数据
     *
     * @param instanceId
     * @param statusList
     * @param finish
     * @return
     */
    @Override
    public List<WorkflowTask> queryByInstanceId(Long instanceId, List<Byte> statusList, Boolean finish) {
        return this.workflowTaskMapper.queryByInstanceId(instanceId, statusList, finish);
    }

    /**
     * 查询指定行数据
     *
     * @param keyword   关键字
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return 对象列表
     */
    @Override
    public List<WorkflowTask> queryByConditions(String keyword, String startDate, String endDate) {
        return this.workflowTaskMapper.queryByConditions(keyword, startDate, endDate);
    }

    /**
     * 根据流程id查询
     *
     * @param workflowId
     * @return
     */
    @Override
    public Long countByWorkflowId(Long workflowId) {
        return this.workflowTaskMapper.countByWorkflowId(workflowId);
    }

    /**
     * 新增数据
     *
     * @param workflowTask 实例对象
     * @return 实例对象
     */
    @Override
    public boolean insert(WorkflowTask workflowTask) {
        return this.workflowTaskMapper.insert(workflowTask) > 0;
    }

    /**
     * 新增数据
     *
     * @param workflowTaskList 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertList(List<WorkflowTask> workflowTaskList) {
        if (this.workflowTaskMapper.insertList(workflowTaskList) > 0) {
            return true;
        }
        throw new RuntimeException();
    }

    /**
     * 修改数据
     *
     * @param workflowTask 实例对象
     * @return 实例对象
     */
    @Override
    public boolean update(WorkflowTask workflowTask) {
        return this.workflowTaskMapper.update(workflowTask) > 0;
    }


    /**
     * 更新任务状态
     *
     * @param taskId
     * @param status
     * @return
     */
    @Override
    public boolean updateTaskStatus(Long taskId, Byte status) {
        return this.workflowTaskMapper.updateTaskStatus(taskId, status) > 0;
    }

    /**
     * 处理任务
     *
     * @param workflowTask
     * @param instanceNodeDetail
     * @param instanceNode
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean handleTask(WorkflowTask workflowTask, InstanceNodeDetail instanceNodeDetail, InstanceNode instanceNode, Instance instance, boolean nodeFinish) {
        if (workflowTaskMapper.updateTaskStatus(workflowTask.getId(), workflowTask.getStatus()) <= 0) {
            return false;
        }
        if (instanceNodeDetailMapper.update(instanceNodeDetail) <= 0) {
            throw new RuntimeException();
        }
        if (instanceNode != null && instanceNodeMapper.update(instanceNode) <= 0) {
            throw new RuntimeException();
        }
        if (instance != null && instanceMapper.updateStageAndStatus(instance.getId(), instance.getStage(), instance.getStatus(), instance.getEndTime()) <= 0) {
            throw new RuntimeException();
        }
        if (nodeFinish && !instanceActuator.performNextInstance(workflowTask.getInstanceId(), instanceNode.getLevel(), instanceNode.getConditionsLevel())) {
            throw new RuntimeException();
        }

        return true;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.workflowTaskMapper.deleteById(id) > 0;
    }

    /**
     * 通过主键删除数据
     *
     * @param idList 主键
     * @return 是否成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIdList(List<Long> idList) {
        if (this.workflowTaskMapper.deleteByIdList(idList) == idList.size()) {
            return true;
        }
        throw new RuntimeException();
    }
}
